<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderLog extends Model
{
    use HasFactory;
    protected $table = 'sp_order_log';
    protected $primaryKey = 'id';
    public $timestamps = false;

    const ORDER_LOG_OPERATE_TYPE_USER = 1;//操作类型用户
    const ORDER_LOG_OPERATE_TYPE_ADMIN = 2;//管理后台
    const ORDER_LOG_PROCESS = 1;//待处理
    //新增Log
    protected function addOrderLog($order_id,$user_id,$operate_type,$content,$status=0){
        $data = [];
        $data['order_id'] = $order_id;
        $data['user_id'] = $user_id;
        $data['operate_type'] = $operate_type;
        $data['content'] = $content;
        $data['user_agent'] =  $_SERVER['HTTP_USER_AGENT'];
        $data['user_ip'] = getRealIp();
        $data['status']=$status;
        $data['create_time'] = date('Y-m-d H:i:s');
        $this->insert($data);
    }

    //通过订单号获取订单操作日志
    protected function getOrderLogByOrderId($order_id){
        $order_log = $this->where('order_id',$order_id)->select('user_id','operate_type','content','create_time')->get();
        $order_log = obToArray($order_log);
        if(!empty($order_log)){
            foreach ($order_log as $k=>$v){
                if($v['operate_type'] == self::ORDER_LOG_OPERATE_TYPE_USER){
                    $order_log[$k]['operate_user'] = '用户';
                }else{
                    $userName = NewAdmin::where('id',$v['user_id'])->value('username');
                    $order_log[$k]['operate_user'] = $userName;
                }
                unset($order_log[$k]['user_id']);
                unset($order_log[$k]['operate_type']);
            }
        }
        return $order_log;
    }

    //通过订单号获取订单操作日志有分页
    protected function getOrderLogByOrderIdPage($order_id,$page,$pageSize){
        $order_log = $this->where('order_id',$order_id)->select('user_id','operate_type','content','create_time')
            ->orderBy('id','desc')->paginate($pageSize);
        $order_log = obToArray($order_log);
        if(!empty($order_log['data'])){
            foreach ($order_log['data'] as $k=>$v){
                if($v['operate_type'] == self::ORDER_LOG_OPERATE_TYPE_USER){
                    $order_log['data'][$k]['operate_user'] = '用户';
                }else{
                    $userName = NewAdmin::where('id',$v['user_id'])->value('username');
                    $order_log['data'][$k]['operate_user'] = $userName;
                }
                unset($order_log['data'][$k]['user_id']);
                unset($order_log['data'][$k]['operate_type']);
            }
        }
        return $order_log;
    }

    /**
     * 审核统计日
     */
    public function getCount($user_id,$date){
        //日总通过数
        if ($orderCreateTime) {
            $orderCreateTime = explode('~', $orderCreateTime);
            $where[] = ['o.create_time', '>=', strtotime($orderCreateTime[0])];
            $where[] = ['o.create_time', '<=', strtotime($orderCreateTime[1])];
        }
        return    (new OrderLog)::where('content','订单审核通过')->distinct('order_id')->where('user_id',$user_id)->whereDate('create_time',$date)->count();

    }
    public function getCheckData($user_id,$date){
          //日审单数
        return (new OrderLog)::whereIn('content','订单转回平台,订单审核通过')->distinct('order_id')->where('user_id',$user_id)->whereDate('create_time',$date)->count();
    }

    public function getCountNumber($user_id,$orderCreateTime){
        //日总通过数
        $where=[];
        if ($orderCreateTime) {
            $orderCreateTime = explode('~', $orderCreateTime);
            $where[] = ['create_time', '>=', $orderCreateTime[0]];
            $where[] = ['create_time', '<=', $orderCreateTime[1]];
        }
        return    (new OrderLog)::where('content','订单审核通过')->distinct('order_id')->where('user_id',$user_id)->where($where)->count();
    }
    public function getCheckDataNumber($user_id,$orderCreateTime){
        //日审单数
        $where=[];
        if ($orderCreateTime) {
            $orderCreateTime = explode('~', $orderCreateTime);
            $where[] = ['create_time', '>=', $orderCreateTime[0]];
            $where[] = ['create_time', '<=', $orderCreateTime[1]];
        }
        return (new OrderLog)::whereIn('content',['订单转回平台','订单审核通过'])->distinct('order_id')->where('user_id',$user_id)->where($where)->count();
    }
    /**
     * 审核统计总
     */
    public function getTotalCount($user_id){
        //总通过数
        $pass_number= (new OrderLog)::where('content','订单审核通过')->distinct('order_id')->where('user_id',$user_id)->count();
        //总审单数
        $count=(new OrderLog)::where('content','订单转回平台')->distinct('order_id')->where('user_id',$user_id)->count();
        $check_number =$pass_number+$count;

        return compact('pass_number','check_number');

    }
}
